﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class PersonnelGroupInsertViewModel : NotificationObject
    {
        PersonnelGroupInsert gWindow;
        PersonnelGroupViewModel gParentViewModel;

        public PersonnelGroupInsertViewModel(PersonnelGroupInsert pWindow, PersonnelGroupViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            Initialize();
        }

        public void Initialize()
        {
            List<WPermission> list = WPermission.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                CheckBox checkBox = new CheckBox();
                checkBox.Margin = new System.Windows.Thickness(5);
                checkBox.Content = list[i].PermissionName.Trim();
                gWindow.PersonnelGroupPermissions.Children.Add(checkBox);
            }
        }

        private String personnelGroupName;
        public String PersonnelGroupName
        {
            get
            {
                return personnelGroupName;
            }
            set
            {
                personnelGroupName = value;
                RaisePropertyChanged("PersonnelGroupName");
            }
        }

        private String personnelGroupPermission;
        public String PersonnelGroupPermission
        {
            get
            {
                return personnelGroupPermission;
            }
            set
            {
                personnelGroupPermission = value;
                RaisePropertyChanged("PersonnelGroupPermission");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(CanInsertCommand));
            }
        }

        protected virtual void CanInsertCommand(object obj)
        {
            //添加用户组
            List<WPersonnelGroup> list = new List<WPersonnelGroup>();
            WPersonnelGroup item = new WPersonnelGroup();
            item.PersonnelGroupName = PersonnelGroupName;
            list.Add(item);

            HttpResponse response = WPersonnelGroup.Insert(list);

            int personnelGroupUID = WPersonnelGroup.GetPersonnelGroupUID(PersonnelGroupName);

            List<WPermission> permissionList = WPermission.GetAll();
            List<WPersonnelGroupPermission> personnelGroupPermissionList = new List<WPersonnelGroupPermission>();

            for (int i = 0; i < gWindow.PersonnelGroupPermissions.Children.Count; i++)
            {
                if (gWindow.PersonnelGroupPermissions.Children[i] is CheckBox)
                {
                    if (((CheckBox)gWindow.PersonnelGroupPermissions.Children[i]).IsChecked == false) continue;

                    String content = ((String)((CheckBox)gWindow.PersonnelGroupPermissions.Children[i]).Content).Trim();
                    for (int j = 0; j < permissionList.Count; j++)
                    {
                        if (content == permissionList[j].PermissionName.Trim())
                        {
                            WPersonnelGroupPermission personnelGroupPermission = new WPersonnelGroupPermission();
                            personnelGroupPermission.PermissionUID = permissionList[j].PermissionUID;
                            personnelGroupPermission.PersonnelGroupUID = personnelGroupUID;
                            personnelGroupPermissionList.Add(personnelGroupPermission);
                            break;
                        }
                    }
                }
            }

            response = WPersonnelGroupPermission.Insert(personnelGroupPermissionList);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(CanQuitCommand));
            }
        }

        protected virtual void CanQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }
    }
}
